﻿namespace IOP.OpenType
{
    /// <summary>
    /// 32-bit signed fixed-point number (16.16)
    /// </summary>
    public readonly struct Fixed
    {
        /// <summary>
        /// 元数据
        /// </summary>
        private readonly uint origin;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="number"></param>
        public Fixed(uint number)
        {
            origin = number;
        }

        /// <summary>
        /// 隐式转换
        /// </summary>
        /// <param name="fixed"></param>
        public static implicit operator double(Fixed @fixed) => ((double)@fixed.origin) / (1 << 16);
        /// <summary>
        /// 隐式转换
        /// </summary>
        /// <param name="number"></param>
        public static implicit operator Fixed(double number) => new Fixed((uint)(number * (1 >> 16)));
        /// <summary>
        /// 隐式转换
        /// </summary>
        /// <param name="number"></param>
        public static implicit operator Fixed(uint number) => new Fixed(number);
        /// <summary>
        /// 隐式转换
        /// </summary>
        /// <param name="number"></param>
        public static implicit operator Fixed(int number) => new Fixed((uint)number);
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            double f = this;
            return f.ToString();
        }
    }
}
